package servlet;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.xml.internal.messaging.saaj.util.Base64;

import dao.UtilisateurDAO;

import model.ShareUser;

/**
 * Servlet implementation class Connexion
 */
@WebServlet("/Connexion")
public class ConnectionUser extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	public static final String VUE = "/WEB-INF/connectionUser.jsp";
	public static final String VUE_INDEX = "/WEB-INF/connectionUser.jsp";

	/**
	 * Default constructor.
	 */
	public ConnectionUser() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		this.getServletContext().getRequestDispatcher(VUE)
				.forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String email = request.getParameter("mailUser");
		String mdp = request.getParameter("mdpUser");
		
		String contextPath = request.getContextPath();
		// System.out.println(email + "---" + mdp);

		UtilisateurDAO uDAO = new UtilisateurDAO();
		ShareUser user = new ShareUser();
		user = uDAO.getUser(email, mdp);

		if (user != null) {
			/* Récupération de la session depuis la requête */
			HttpSession session = request.getSession();

			System.out.println(user.getLogin());
			
			// l'utilisateur est l'administrateur
			if (user.getLogin().equals("admin")) {
				session.setAttribute("admin", user.getLogin());
				int idUser = user.getIdUser();
				String idU = Integer.toString(idUser);
				// System.out.println(idUser);

				// creation d'un cookie
				Cookie cookie = new Cookie("idUser", idU);
				response.addCookie(cookie);
				
				// on appelle l'autre servlet
				response.sendRedirect(response.encodeRedirectURL(contextPath
						+ "/listUser"));
				
			} else {
				System.out.println("Connexion OK");
				session.setAttribute("user", user.getLogin());
				int idUser = user.getIdUser();
				String idU = Integer.toString(idUser);
				// System.out.println(idUser);

				// creation d'un cookie
				Cookie cookie = new Cookie("idUser", idU);
				response.addCookie(cookie);

				// on appelle l'autre servlet
				response.sendRedirect(response.encodeRedirectURL(contextPath
						+ "/sentShare"));
			}
		} else {
			
			response.sendRedirect(response.encodeRedirectURL(contextPath
					+ "/connectionUser"));
			System.out.println("Connexion echoué");
		}
	}

}
